package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.AgencyCertificationAuditMapper;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.CertificationAuditReqDTO;
import com.jzo2o.customer.model.dto.response.RejectReasonResDTO;
import com.jzo2o.customer.service.AgencyCertificationAuditService;
import com.jzo2o.customer.service.IAgencyCertificationService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class AgencyCertificationAuditServiceImpl extends ServiceImpl<AgencyCertificationAuditMapper,
                                      AgencyCertificationAudit> implements AgencyCertificationAuditService {

    @Resource
    IAgencyCertificationService agencyCertificationService;

    @Override
    @Transactional
    public void agencyCertificationAudit(AgencyCertificationAuditAddReqDTO agencyCertificationAuditAddReqDTO) {

        AgencyCertificationAudit agencyCertificationAudit =
                BeanUtil.toBean(agencyCertificationAuditAddReqDTO, AgencyCertificationAudit.class);
        agencyCertificationAudit.setAuditStatus(0);
        baseMapper.insert(agencyCertificationAudit);

        Long serveProviderId = agencyCertificationAudit.getServeProviderId();
        AgencyCertification byId = agencyCertificationService.getById(serveProviderId);

        if(ObjectUtil.isNotEmpty(byId)){
            byId.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
            agencyCertificationService.updateById(byId);
        }else {

            byId = new AgencyCertification();
            byId.setId(serveProviderId);
            byId.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
            agencyCertificationService.save(byId);

        }

    }



    @Override
    public RejectReasonResDTO rejectReason() {

        Long userId = UserContext.currentUserId();

        AgencyCertificationAudit agencyCertificationAudit = baseMapper.selectOne(new QueryWrapper<AgencyCertificationAudit>()
                .lambda().eq(AgencyCertificationAudit::getServeProviderId, userId));

        RejectReasonResDTO rejectReasonResDTO = new RejectReasonResDTO();
        rejectReasonResDTO.setRejectReason(agencyCertificationAudit.getRejectReason());


        return rejectReasonResDTO;
    }

    /**
     * @description:  机构认证列表
     * @author: xushuaihang
     * @Date: 2024/8/21 15:55
     * @param:
     * @param agencyCertificationAuditPageQueryReqDTO
     * @return: com.jzo2o.common.model.PageResult<com.jzo2o.customer.model.domain.AgencyCertificationAudit>
    **/
    @Override
    public PageResult<AgencyCertificationAudit> pageList(AgencyCertificationAuditPageQueryReqDTO agencyCertificationAuditPageQueryReqDTO) {

        Page<AgencyCertificationAudit> page = PageUtils.parsePageQuery(agencyCertificationAuditPageQueryReqDTO, AgencyCertificationAudit.class);

        Page<AgencyCertificationAudit> pageList = baseMapper.selectPage(page, null);

        return PageUtils.toPage(pageList,AgencyCertificationAudit.class);
    }

    /**
     * @description: 机构人员信息认证审核状态
     * @author: xushuaihang
     * @Date: 2024/8/21 19:34
     * @param:
     * @param id
     * @param certificationAuditReqDTO
     * @return: void
    **/
    @Override
    public void auditStatus(Long id, CertificationAuditReqDTO certificationAuditReqDTO) {

    }
}
